package com.xinxin.leetcode.problem563;

import com.xinxin.leetcode.common.TreeNode;
import com.xinxin.leetcode.common.TreeUtil;

/**
 * @author ShiXinXin
 * @date 2020-01-29 23:13
 */
class Solution {
    private int sum(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int l = sum(root.left);
        int r = sum(root.right);
        result += Math.abs(l - r);
        return l + r + root.val;
    }

    int result = 0;

    public int findTilt(TreeNode root) {
        sum(root);
        return result;
    }

    public static void main(String[] args) {
        TreeNode root = TreeUtil.createTree(new int[]{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0});
        System.out.println(new Solution().findTilt(root));
    }
}
